package com.sf.ams.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年9月27日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
public class FileUtil {
	private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
	private static final String fileNameSpiltChar = "-";
	/**
	 * 把文件里的数据取出放入String中其中Map 中的key为文件名的首个数字
	 * @param files
	 * @param currentIndex 过滤掉currentIndex之前的数据
	 * @return
	 */
	public static Map<Integer,String> transferFileToString(List<File> files,Integer currentIndex) {
		Map<Integer,String> result = new HashMap<>();
		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		List<File> childFilesList = files;
		childFilesList.stream().sorted((f1,f2) -> f1.getName().compareTo(f2.getName()));
		for(int i = 0;i < childFilesList.size();i++ ){
			
			try {
				String fileName = childFilesList.get(i).getName();
				if(fileName.indexOf(fileNameSpiltChar) == -1){
					logger.info("脚本文件{}不符合命名规范，将被过滤掉不执行",fileName);
					continue;
				}
				String fileIndex = fileName.substring(0, fileName.indexOf(fileNameSpiltChar));
				Integer iFileIndex = null;
				try {
					iFileIndex = Integer.valueOf(fileIndex);
				} catch (NumberFormatException e) {
					logger.info("脚本文件{}不符合命名规范，将被过滤掉不执行",fileName);
					continue;
				}
				inputStream = new FileInputStream(childFilesList.get(i));
				inputStreamReader = new InputStreamReader(inputStream,"utf-8");
				String temp = null;
				StringBuilder sql = new StringBuilder();
				reader = new BufferedReader(inputStreamReader);
				while ((temp = reader.readLine()) != null) {
					sql.append(temp).append("\r\n");
				}
				if (StringUtil.isNotEmpty(sql.toString()) && Integer.valueOf(iFileIndex).compareTo(currentIndex) >= 0) {
					result.put(Integer.valueOf(fileIndex),sql.toString());
				}
				 
			} catch (FileNotFoundException e) {
				logger.error("class FileUtil method transferFileToString file not found",e);
			} catch (UnsupportedEncodingException e) {
				logger.error("class FileUtil method transferFileToString UnsupportedEncoding",e);
			} catch (IOException e) {
				logger.error("class FileUtil method transferFileToString IOException",e);
			} finally {
				try {
					if(reader != null) {
						reader.close();
					}
					if(inputStreamReader != null) {
						inputStreamReader.close();
					}
					if(inputStream != null) {
						inputStream.close();
					}
				} catch (IOException e) {
					logger.error(e.getMessage(),e);
				}
			}
		}
		return result;
	}
}
